package com.ja.openchat.ui.found;

import android.text.TextUtils;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResponse;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.circle.UserTagListInfo;
import com.zy.openchat.core.model.circle.UserTags;
import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.util.Common;

import java.util.ArrayList;
import java.util.Map;

public class UserCircleManager {


    private volatile static UserCircleManager tagManager;
    public static UserCircleManager getIntance() {
        if (tagManager == null) {
            synchronized (UserCircleManager.class) {
                if (tagManager == null) {
                    tagManager = new UserCircleManager();
                }
            }
        }
        return tagManager;
    }


    private UserCircleManager(){

    }


    public void createTag(String name,ArrayList<UserInfo> userInfos,UserTagCallback callback){
        if(TextUtils.isEmpty(name)||userInfos==null||userInfos.size()==0){
            return ;
        }
        UserTags userTags = new UserTags(name,userInfos);

        TelegramManager.getTelegramManager().createBlogUserGroup(ActivityUtils.getTopActivity(), name, userInfos, new TgResultBlock<UserTags>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, UserTags obj) {
                if(obj!=null){
                    userTags.id = obj.id;
                    callback.onBack(userTags);
                }else{
                    callback.onError();
                    ToastUtils.showShort(ActivityUtils.getTopActivity().getString(R.string.request_error));
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                callback.onError();
                ToastUtils.showShort(ActivityUtils.getTopActivity().getString(R.string.request_error));
            }
        });

//        save();
        return ;
    }

    public void editUserTags(UserTags preUserTags,UserTags userTags,UserTagCallback callback){
        if(userTags==null){
            return;
        }
        TelegramManager.getTelegramManager().editBlogUserGroup(ActivityUtils.getTopActivity(),preUserTags,userTags, new TgResultBlock<TgResponse>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                if(obj!=null&&obj.ok){
                    callback.onBack(userTags);
                }else{
                    callback.onError();
                    ToastUtils.showShort(ActivityUtils.getTopActivity().getString(R.string.request_error));
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                callback.onError();
                ToastUtils.showShort(ActivityUtils.getTopActivity().getString(R.string.request_error));
            }
        });
    }

    public void deleteUserTags(UserTags userTags,UserTagCallback callback){
        if(userTags==null){
            return;
        }
        TelegramManager.getTelegramManager().deleteBlogUserGroup(ActivityUtils.getTopActivity(),userTags.id, new TgResultBlock<TgResponse>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                if(obj!=null&&obj.ok){
                    callback.onBack(userTags);
                }else{
                    callback.onError();
                    ToastUtils.showShort(ActivityUtils.getTopActivity().getString(R.string.request_error));
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                callback.onError();
                ToastUtils.showShort(ActivityUtils.getTopActivity().getString(R.string.request_error));
            }
        });
    }

    public void clean(){
//        mUserTags = new ArrayList<UserTags>();
//        save();
    }


    public void getUserTagsByType(ArrayList<UserTags> userTags,int type,ArrayList<UserTags> chooseUserTags,UserTagsCallback callback){
        if(userTags==null){
            callback.onBack(userTags);
            return;
        }
        userTags.clear();
        TelegramManager.getTelegramManager().getBlogUserGroups(ActivityUtils.getTopActivity(), new TgResultBlock<UserTagListInfo>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, UserTagListInfo obj) {
                if(obj!=null&&!Common.isEmpty(obj.groups)){
                    for(UserTags tags:obj.groups){
                        tags.isChecked = false;
                        if(!Common.isEmpty(chooseUserTags)){
                            for(UserTags innerTags:chooseUserTags){
                                if(innerTags.id == tags.id){
                                    tags.isChecked = true;
                                    break;
                                }
                            }
                        }
                        userTags.add(tags);
                    }
                }
                callback.onBack(userTags);
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                callback.onBack(userTags);
            }
        });
    }



}
